<?php
class Admin_MessageController extends Zendhlt_Controller_AdminAction {

	const ADMIN_ROLE=1;

	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;

	//Duong dan cua Controller
	protected $_currentController;

	//Duong dan cua Action chinh
	protected $_actionMain;

	//Admin user dang login
	protected $_crrUsra;

	//Thong so phan trang
	protected $_paginator = array('itemCountPerPage' => 5, 'pageRange' => 3);
	protected $_namespace;

	public function init() {
		$file_uri = $this->view->baseUrl(PUBLIC_PATH . '/js/ajaxupload.3.5.js');
		$this->view->headScript()->appendFile($file_uri);
		$layoutPath = TEMPLATE_PATH  . '/admin';
		$option = array ('layout' => 'index',
                   'layoutPath' => $layoutPath );
		Zend_Layout::startMvc ( $option );

		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();

		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module']
		. '/' . $this->_arrParam['controller'];

		//Duong dan cua Action chinh
		$this->_actionMain = '/' . $this->_arrParam['module']
		. '/' . $this->_arrParam['controller'];

		$this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
		$this->_arrParam['paginator'] = $this->_paginator;

		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();

		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
			$ssFilter->col 			= 'id';
			$ssFilter->order 		= 'DESC';
		}
		$this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
		$this->_arrParam['ssFilter']['col'] 	 = $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 	 = $ssFilter->order;

		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		//Lay thong tin admin user hien tai
		$auth = Zend_Auth::getInstance();
		$this->_crrUsra = $auth->getIdentity();

	}

	public function indexAction() {
		$this->_helper->viewRenderer->setNoRender();
	}
	public function sendAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();

		$form = new Admin_Form_SendMsg();
			
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$msg = new Admin_Model_TableColumn_TinNhan($formData );
				$msgMapper  = new Admin_Model_TinNhanMapper();
				if($msgMapper->save($msg))
				$this->view->msg="Gửi tin nhắn thành công";
				else{
					$this->view->msg="Gửi tin nhắn thất bại! Vui lòng thử lại";
				}
				$this->_redirect("/admin/ptrtinrao");
			}
		}

	}
	public function validateAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$form = new Admin_Form_SendMsg();
		$form->isValid($this->_getAllParams());
		$json = $form->getMessages();
		header('Content-type: application/json');
		echo Zend_Json::encode($json);

	}
	public function sendajaxAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$data = $this->_request->getPost();
		if ($data['tieu_de']!=null&&$data['noi_dung']!=null) {
			$msg = new Admin_Model_TableColumn_TinNhan($data );
			$msgMapper  = new Admin_Model_TinNhanMapper();
			if($msgMapper->save($msg))
				$sts="OK";
			
		}
		$json = array('sts'=>$sts);
		header('Content-type: application/json');
		echo Zend_Json::encode($json);

	}

}